package com.chenyun.boot.web.config;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 解决文件读取一次后再此读取为空报错问题
 * @author: lonni
 * @CreateTime: 2023-12-02  19:14
 * @Description:
 */
public class InputStreamBodyFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ServletRequest requestWrapper=null;
        if(request instanceof HttpServletRequest) {
            requestWrapper=new InputStreamHttpServletRequestWrapper((HttpServletRequest)request);
        }
        if(requestWrapper==null) {
            filterChain.doFilter(request, response);
        }else {
            filterChain.doFilter(requestWrapper, response);
        }
    }
}
